// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#include "MgcImage.h"

MgcImplementRTTI(MgcImage,MgcObject);
MgcImplementStream(MgcImage);

unsigned int MgcImage::ms_auiBytesPerPixel[MgcImage::IT_QUANTITY] =
{
    2, 3, 4, 4
};

//---------------------------------------------------------------------------
MgcImage::MgcImage (Type eType, unsigned int uiWidth, unsigned int uiHeight,
    unsigned char* aucData)
{
    m_eType = eType;
    m_uiWidth = uiWidth;
    m_uiHeight = uiHeight;
    m_uiQuantity = uiWidth*uiHeight;
    m_aucData = aucData;
}
//---------------------------------------------------------------------------
MgcImage::MgcImage ()
{
    SetDefault();
}
//---------------------------------------------------------------------------
MgcImage::~MgcImage ()
{
    delete[] m_aucData;
}
//---------------------------------------------------------------------------
void MgcImage::SetDefault ()
{
    m_eType = IT_QUANTITY;
    m_uiWidth = 0;
    m_uiHeight = 0;
    m_uiQuantity = 0;
    m_aucData = 0;
}
//---------------------------------------------------------------------------
MgcImage::MgcImage (const char* acFilename)
{
    assert( acFilename );
    if ( !acFilename )
    {
        SetDefault();
        return;
    }

    FILE* pkFile;
    if ( fopen_s(&pkFile, acFilename,"rb") )
	{
        SetDefault();
        return;
    }

    unsigned int uiType;
    int iScanned = fscanf_s(pkFile,"Magic3D Image File#%u#%u#%u#",&uiType,
        &m_uiWidth,&m_uiHeight);

    if ( iScanned == EOF
    ||   iScanned != 3
    ||   uiType >= (unsigned int) IT_QUANTITY
    ||   m_uiWidth == 0
    ||   m_uiHeight == 0 )
    {
        SetDefault();
        return;
    }

    m_eType = (Type) uiType;
    m_uiQuantity = m_uiWidth*m_uiHeight;

    unsigned int uiSize = ms_auiBytesPerPixel[m_eType]*m_uiQuantity;
    m_aucData = new unsigned char[uiSize];

    unsigned int uiRead = fread(m_aucData,sizeof(unsigned char),uiSize,
        pkFile);

    if ( uiRead != uiSize )
    {
        delete[] m_aucData;
        SetDefault();
        return;
    }
}
//---------------------------------------------------------------------------
bool MgcImage::Save (const char* acFilename)
{
    assert( acFilename );
    if ( !acFilename )
        return false;

    FILE* pkFile;
    if ( fopen_s(&pkFile, acFilename,"wb") )
        return false;

    // write header
    char acHeader[64];
    sprintf_s(acHeader,64,"Magic3D Image File#%u#%u#%u#",(unsigned int)m_eType,
        m_uiWidth,m_uiHeight);
    fwrite(acHeader,sizeof(char),strlen(acHeader),pkFile);

    // write data
    unsigned int uiSize = ms_auiBytesPerPixel[m_eType]*m_uiQuantity;
    fwrite(m_aucData,sizeof(unsigned char),uiSize,pkFile);

    fclose(pkFile);
    return true;
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// streaming
//---------------------------------------------------------------------------
MgcObject* MgcImage::Factory (MgcStream& rkStream)
{
    MgcImage* pkObject = new MgcImage;
    MgcStream::Link* pkLink = new MgcStream::Link(pkObject);
    pkObject->Load(rkStream,pkLink);
    return pkObject;
}
//---------------------------------------------------------------------------
void MgcImage::Load (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcObject::Load(rkStream,pkLink);

    // native data
    MgcStreamRead(rkStream,m_eType);
    MgcStreamRead(rkStream,m_uiWidth);
    MgcStreamRead(rkStream,m_uiHeight);
    MgcStreamRead(rkStream,m_uiQuantity);

    MgcStreamRead(rkStream,m_aucData);
    if ( m_aucData )
    {
        unsigned int uiBytes =
            ms_auiBytesPerPixel[m_eType]*m_uiWidth*m_uiHeight;
        m_aucData = new unsigned char[uiBytes];
        MgcStreamRead(rkStream,m_aucData,uiBytes);
    }
}
//---------------------------------------------------------------------------
void MgcImage::Link (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcObject::Link(rkStream,pkLink);
}
//---------------------------------------------------------------------------
bool MgcImage::Register (MgcStream& rkStream)
{
    return MgcObject::Register(rkStream);
}
//---------------------------------------------------------------------------
void MgcImage::Save (MgcStream& rkStream)
{
    MgcObject::Save(rkStream);

    // native data
    MgcStreamWrite(rkStream,m_eType);
    MgcStreamWrite(rkStream,m_uiWidth);
    MgcStreamWrite(rkStream,m_uiHeight);
    MgcStreamWrite(rkStream,m_uiQuantity);

    MgcStreamWrite(rkStream,m_aucData);
    if ( m_aucData )
    {
        unsigned int uiBytes =
            ms_auiBytesPerPixel[m_eType]*m_uiWidth*m_uiHeight;
        MgcStreamWrite(rkStream,m_aucData,uiBytes);
    }
}
//---------------------------------------------------------------------------
